package cn.haohaowo.controller;

import java.util.ArrayList;
import java.util.List;

import cn.haohaowo.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

import cn.haohaowo.common.Page;
import cn.haohaowo.entity.Book;
import cn.haohaowo.service.BookBizService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Controller
@RequestMapping("/book")
public class BookController {

	private static final long serialVersionUID = -4681405306237469913L;

	private Integer id;
	private String keyword = "";
	private String type = "";
	private String title = "";
	private String author = "";
	private String publisher = "";
	private String minprice = "";
	private String maxprice = "";
	private Integer pageNo = 1;
	private Page<Book> page;
	private Book book;
	private List<Book> recommendedBooks = new  ArrayList<Book>();
	
	@Autowired
	private BookBizService bookBizService;

    @RequestMapping("/list.do")
	public String list(HttpServletRequest request, HttpServletResponse response){
		page = bookBizService.getBooks(type, title, author, publisher, minprice, maxprice, pageNo);
		page.setUrl(request.getRequestURI());
		
		return Constants.LIST;
	}

    @RequestMapping("/search.do")
	public String search(HttpServletRequest request, HttpServletResponse response){
		page = bookBizService.search(keyword, pageNo);
		page.setUrl(request.getRequestURL().toString());
		
		return Constants.SEARCH;
	}

    @RequestMapping("/show.do")
	public String show(HttpServletRequest request, HttpServletResponse response){
		book = bookBizService.getBook(id);
		if(null == book){
			return Constants.BOOK_NOT_FOUND;
		}else{
			recommendedBooks = bookBizService.getRelativeBooks(id);
			return Constants.SHOW;
		}
	}
	
}
